package cn.com.jslh.boarpro.util;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.model.breeditems.ChoseBreedItem;
import cn.com.jslh.boarpro.main.model.breeditems.DateBreedItem;
import cn.com.jslh.boarpro.main.model.breeditems.EditTextBreedItem;
import cn.com.jslh.boarpro.main.model.breeditems.TextBreedItem;
import cn.com.jslh.boarpro.util.timedialog.DatePickerListener;
import cn.com.jslh.boarpro.util.timedialog.DateChoseDialogUtil;

/**
 * 作者：周洲 on 2017/8/2 13:58
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class BreedingTitleUtil {

    public interface TextBreedItemListener{
        public void onInject(TextBreedItem data, IViewInjector injector);
    }
    public interface DateBreedItemListener{
        public void onInject(DateBreedItem data, IViewInjector injector,String dateStr);
    }
    public interface ChoseBreedItemListener{
        public void onInject(ChoseBreedItem data, IViewInjector injector);
    }
    public interface EditTextBreedItemListener{
        public void onInject(EditTextBreedItem data, String textValue);
    }
    public static SlimAdapterEx getBreedingTitleAdapter(final Context context, RecyclerView recyclerView,
                                                        final TextBreedItemListener textBreedItemListener,
                                                        final DateBreedItemListener dateBreedItemListener,
                                                        final ChoseBreedItemListener choseBreedItemListener,
                                                        final EditTextBreedItemListener editTextBreedItemListener
                                                        ){
        return SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .registerDefault(R.layout.breeding_record_item_text_layout, new SlimInjector<TextBreedItem>() {
                    @Override
                    public void onInject(TextBreedItem data, IViewInjector injector) {
                        injector.text(R.id.title, data.getTitle() + "：")
                                .text(R.id.valueText, data.getValueStr());
                        if(textBreedItemListener!=null)
                            textBreedItemListener.onInject(data,injector);
                    }
                })
                .register(R.layout.breeding_record_item_date_layout, new SlimInjector<DateBreedItem>() {
                    @Override
                    public void onInject(final DateBreedItem data, final IViewInjector injector) {
                        injector.text(R.id.title, data.getTitle() + "：")
                                .text(R.id.valueText, data.getValueStr())
                                .clicked(R.id.valueText, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        DateChoseDialogUtil.showTimeDialog(context, new DatePickerListener() {
                                            @Override
                                            public void picker(String date) {
                                                if(dateBreedItemListener!=null)
                                                    dateBreedItemListener.onInject(data,injector,date);
                                            }
                                        });
                                    }
                                });
                    }
                })
                .register(R.layout.breeding_record_item_chose_layout, new SlimInjector<ChoseBreedItem>() {
                    @Override
                    public void onInject(final ChoseBreedItem data, final IViewInjector injector) {
                        injector.text(R.id.title, data.getTitle() + "：")
                                .text(R.id.valueText, data.getValueStr())
                                .clicked(R.id.valueText, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if(choseBreedItemListener!=null)
                                            choseBreedItemListener.onInject(data,injector);
                                    }
                                });
                    }
                })
                .register(R.layout.breeding_record_item_edit_layout, new SlimInjector<EditTextBreedItem>() {
                    @Override
                    public void onInject(final EditTextBreedItem data, IViewInjector injector) {
                        injector.text(R.id.title, data.getTitle() + "：")
                                .with(R.id.valueText, new IViewInjector.Action() {
                                    @Override
                                    public void action(View view) {
                                        ((EditText)view).setHint(data.getValueStr());
                                    }
                                });
                        injector.with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                ((EditText)view).addTextChangedListener(new TextWatcher() {
                                    @Override
                                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                                    }

                                    @Override
                                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                                    }

                                    @Override
                                    public void afterTextChanged(Editable editable) {
                                        if(editTextBreedItemListener!=null)
                                            editTextBreedItemListener.onInject(data,editable.toString());
                                    }
                                });
                            }
                        });
                    }
                })
                .attachTo(recyclerView);
    }
}
